package com.campus.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.campus.dao.LoginDao;
import com.campus.entity.Login;

@Repository
public class LoginDaoImpl implements LoginDao {

	@Resource
	private SessionFactory sessionFactory;
	
	@Override
	public List<Login> selectList() {
		List<Login> list = (List<Login>) sessionFactory.getCurrentSession().createQuery("from Login").list();
		return list;
	}

	@Override
	public void add(Login thisClass) {
		sessionFactory.getCurrentSession().save(thisClass);
	}

	@Override
	public Login getById(int id) {
		Login myClass = (Login) sessionFactory.getCurrentSession().get(Login.class, id);
		return myClass;
	}

	@Override
	public void update(Login thisClass) {
		sessionFactory.getCurrentSession().update(thisClass);
	}

	@Override
	public void deleteById(int id) {
		Login c=new Login();
		c.setId(id);
		sessionFactory.getCurrentSession().delete(c);
	}

	@Override
	public int countAll() {
		long n = (Long) sessionFactory.getCurrentSession().createQuery("Select count(u) from Login u").uniqueResult();
		return (int)n;
	}

	@Override
	public Login chekeLogin(String username, String password) {
		List<Login> list = sessionFactory.getCurrentSession().createQuery("from Login where username=? and password=?")
		.setParameter(0,username)
		.setParameter(1,password)
		.list();
		
		if (!list.equals(0)) {
			for(Login login : list) {
				return login;
			}
		}
		return null;
	}

	@Override
	public int chekeLoginName(String username) {
		List<Login> list = sessionFactory.getCurrentSession().createQuery("from Login where username=?")
				.setParameter(0,username)
				.list();
				
		if (list.size()==0) {
			return 0;
		}else {
			return 1;
		}
		
	}





}
